#include <kew/c-vm.h>

void kew_object_add_extension(KewObject *object, gpointer key, gpointer value)
{
    if (! object->extensions)
        object->extensions = g_hash_table_new();

    g_hash_table_insert(object->extensions, key, value);
}


void kew_object_remove_extension(KewObject *object, gpointer key)
{
    if (object->extensions) {
        g_hash_table_remove(object->extensions, key);
        if (g_hash_table_size(object->extensions) == 0) {
            g_hash_table_destroy(object->extensions);
            object->extensions = NULL;
    }
}

gpointer kew_object_get_extension(KewObject *object, gpointer key)
{
    if (! object->extensions)
        return NULL;

    return g_hash_table_lookup(object->extensions, key);
}

void kew_object_type_add_extension(KewObjectType *type, gpointer key, gpointer value)
{
    if (! type->extensions)
        type->extensions = g_hash_table_new();

    g_hash_table_insert(type->extensions, key, value);
}


void kew_object_type_remove_extension(KewObjectType *type, gpointer key)
{
    if (type->extensions) {
        g_hash_table_remove(type->extensions, key);
        if (g_hash_table_size(type->extensions) == 0) {
            g_hash_table_destroy(type->extensions);
            type->extensions = NULL;
    }
}

gpointer kew_object_type_get_extension(KewObjectType *type, gpointer key)
{
    if (! type->extensions)
        return NULL;

    return g_hash_table_lookup(type->extensions, key);
}
